package com.czl.module_storehouse.adapter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.ReceiveBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.bean.StockInfoBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class StockInfoAdapter extends BaseMultiItemQuickAdapter<StockInfoBean, BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public StockInfoAdapter(List<StockInfoBean> data) {
        super(data);
        addItemType(StockInfoBean.TYPE_HEADER, R.layout.item_text_stock);
        addItemType(StockInfoBean.TYPE_LOCATION, R.layout.item_stock_location);
        addItemType(StockInfoBean.TYPE_RECEIVE, R.layout.item_stock_receive);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, StockInfoBean item) {
        switch (item.getItemType()) {
            case StockInfoBean.TYPE_HEADER:
                helper.setText(R.id.tv_header, item.getHeaderTitle());
                break;
            case StockInfoBean.TYPE_LOCATION:
                helper.setText(R.id.tv_left, item.getLeftName())
                        .setText(R.id.tv_right, item.getRightName())
                        .setVisible(R.id.sv_right, !item.isLastLocation());
                break;
            case StockInfoBean.TYPE_RECEIVE:
                helper.setText(R.id.tv_user, item.getUserName())
                        .setText(R.id.tv_date, item.getDate())
                        .setText(R.id.tv_num, item.getNum());
                View view = helper.getView(R.id.ll_content);
                view.setBackgroundResource(item.isHeader() ? R.drawable.shape_stock_receive_info : 0);
                break;
            default:
                break;
        }
    }

    public void addAll(Context context, SortBean sortBean) {
        List<StockInfoBean> newList = new ArrayList<>();
        List<LocatListBean> locatList = sortBean.getLocatList();
        //库存信息
        StockInfoBean stockInfoBean = new StockInfoBean(StockInfoBean.TYPE_HEADER);
        newList.add(stockInfoBean);
        int stockTotalNum = 0;
        if (locatList != null) {
            int stockNum = locatList.size();
            StockInfoBean infoLocatBean = null;
            for (int i = 0; i < stockNum; i++) {
                LocatListBean bean = locatList.get(i);
                stockTotalNum += bean.getStockNumInt();
                if (i % 2 == 0) {
                    infoLocatBean = new StockInfoBean(StockInfoBean.TYPE_LOCATION);
                    infoLocatBean.setLeftName(bean.getStorehouseLocatName() + "(" + bean.getStockNumInt() + ")");
                } else {
                    infoLocatBean.setRightName(bean.getStorehouseLocatName() + "(" + bean.getStockNumInt() + ")");
                    newList.add(infoLocatBean);
                }
                if (i == stockNum - 1 && i % 2 == 0) {
                    infoLocatBean.setLastLocation(true);
                    newList.add(infoLocatBean);
                }
            }
            for (LocatListBean bean : locatList) {
                StockInfoBean infoBean = new StockInfoBean(StockInfoBean.TYPE_LOCATION);
                infoBean.setLocatListBean(bean);
            }
        }

        stockInfoBean.setHeaderTitle(context.getString(R.string.stock_num_risk, stockTotalNum));

        //领用信息
        List<ReceiveBean> receiveList = sortBean.getReceiveList();
        StockInfoBean receiveInfoBean = new StockInfoBean(StockInfoBean.TYPE_HEADER);
        newList.add(receiveInfoBean);
        //添加默认信息
        StockInfoBean receiveHeaderBean = new StockInfoBean(StockInfoBean.TYPE_RECEIVE);
        receiveHeaderBean.setHeader(true);
        receiveHeaderBean.setUserName("领用人");
        receiveHeaderBean.setDate("领用时间");
        receiveHeaderBean.setNum("未还数量");
        newList.add(receiveHeaderBean);
        int receiveNum = 0;
        if (receiveList != null) {
            for (ReceiveBean bean : receiveList) {
                StockInfoBean infoBean = new StockInfoBean(StockInfoBean.TYPE_RECEIVE);
                infoBean.setReceiveBean(bean);
                receiveNum += bean.getNum();
                newList.add(infoBean);
            }
        }
        receiveInfoBean.setHeaderTitle(context.getString(R.string.receive_num, receiveNum));
        //借用信息
        List<BorrowBean> borrowList = sortBean.getBorrowList();
        StockInfoBean borrowInfoBean = new StockInfoBean(StockInfoBean.TYPE_HEADER);
        newList.add(borrowInfoBean);
        //添加默认信息
        StockInfoBean borrowHeaderBean = new StockInfoBean(StockInfoBean.TYPE_RECEIVE);
        borrowHeaderBean.setHeader(true);
        borrowHeaderBean.setUserName("借用人");
        borrowHeaderBean.setDate("借用时间");
        borrowHeaderBean.setNum("未还数量");
        newList.add(borrowHeaderBean);
        int borrowNum = 0;
        if (borrowList != null) {
            for (BorrowBean bean : borrowList) {
                StockInfoBean infoBean = new StockInfoBean(StockInfoBean.TYPE_RECEIVE);
                infoBean.setBorrowBean(bean);
                borrowNum += bean.getNum();
                newList.add(infoBean);
            }
        }
        borrowInfoBean.setHeaderTitle(context.getString(R.string.borrow_num, borrowNum));
        addData(newList);
    }
}
